package com.yy.finvideoplayer.ext

/**
 * @Description: DensityExtensions
 * @Author: yiw89
 * @CreateDate: 2025/9/13 0:06
 * @Version: 1.0
 */
import android.content.Context

// DP 转 PX 扩展函数
fun Int.dp2pixels(context: Context): Int {
    val density = context.resources.displayMetrics.density
    return (this * density).toInt()
}

fun Float.dp2pixels(context: Context): Int {
    val density = context.resources.displayMetrics.density
    return (this * density).toInt()
}

// 处理可为空的版本
fun Int?.dp2pixels(context: Context): Int {
    return this?.let {
        (it * context.resources.displayMetrics.density).toInt()
    } ?: 0
}

fun Float?.dp2pixels(context: Context): Int {
    return this?.let {
        (it * context.resources.displayMetrics.density).toInt()
    } ?: 0
}

// orZero() 扩展函数
fun Int?.orZero(): Int = this ?: 0
fun Float?.orZero(): Float = this ?: 0f